home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: cwi.nl!dik
- From: dik@cwi.nl (Dik T. Winter)
- Subject: Re: Problem Negating an Unsigned Char
- Message-ID: <Dntr8t.L41@cwi.nl>
- Sender: news@cwi.nl (The Daily Dross)
- Nntp-Posting-Host: chrysant.cwi.nl
- Organization: CWI, Amsterdam
- References: <4he27sINNdel@keats.ugrad.cs.ubc.ca> <DnqMyr.4pF@cwi.nl> <4hfmmgINN4t8@anvil.ugrad.cs.ubc.ca>
- Date: Wed, 6 Mar 1996 02:36:29 GMT
-
- In article <4hfmmgINN4t8@anvil.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
- > In article <DnqMyr.4pF@cwi.nl>, Dik T. Winter <dik@cwi.nl> wrote:
- > > > To make it portable, you must manually mask for the lower eight bits:
- > > >
- > > > if (a == (~b & 0xff))
- > >
- > >And how would this work on machines with other than eight bit chars?
- >
- > Just fine, thank you. The C standard guarantees char to be at least eight
- > bits wide. The original poster wants eight bit arithmetic; i.e. that the
- > complement of 0x11 be equal to 0xEE. This is what he is testing for, and
- > the above is how you get it.
-
- Well, that is your interpretation of what the original poster wants. See
- the subject line. As I read it he just wanted to know why
- a == ~b
- does not work and why
- a == (unsigned char)(~b)
- does work.
- ...
- > Casting to an unsigned quantity is implementation defined. Are you suggesting
- > that the invocation of implementation defined behavior is more portable than a
- > well-defined bit masking operation?
-
- I know that negating unsigned chars with CHAR_BIT bits is difficult to do
- portably (see for instance Peter Seebach's posts). And, yes, I know that
- casting is likely to fail on a 1's complement machine. But of the 1's
- complement machines I ever did use, only one had C implemented and that
- was unusable as a language on that machine (due to the problems to mimic
- mod 2 arithmetic on unsigned amongst others). As soon as somebody comes
- up with a 1's complement machine with a usable C implementation I may
- change my viewpoint.
- --
- dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924098
- home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
-